﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using Server.App_Data;
using System.Linq;
using System.Data.Objects.DataClasses;

namespace Server
{
    // REMARQUE : vous pouvez utiliser la commande Renommer du menu Refactoriser pour changer le nom de classe "Service1" dans le code, le fichier svc et le fichier de configuration.
    public class Service : IService
    {
        // Instance de la BDD
        ModelContainer db = new ModelContainer();

        // pubFeeds => tous les feeds que l'utilisateur a publié


        #region User

        /// <summary>
        /// Ajoute un utilisateur dans la base de données
        /// </summary>
        /// <param name="email">Obligatoire</param>
        /// <param name="password">Obligatoier</param>
        /// <param name="question">Facultatif</param>
        /// <param name="answer">Facultatif</param>
        public bool RegisterUser(string email, string password, string question, string answer)
        {
            bool registerSuccess = false;
            if (!string.IsNullOrEmpty(email) && !string.IsNullOrEmpty(password) && !EmailExists(email))
            {
                registerSuccess = true;
                User newUser = new User() { Email = email, Password = password, Question = question, Answer = answer };
                db.AddToUserJeu(newUser);
                db.SaveChanges();
            }
            return registerSuccess;
        }

        /// <summary>
        /// Vérifie si l'email existe en base de données
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        public bool EmailExists(string email)
        {
            bool exists = false;
            if (db.UserJeu != null)
                exists = (db.UserJeu.FirstOrDefault(u => u.Email == email) != null);
            return exists;
        }


        /// <summary>
        /// Vérifie si l'utilisateur existe
        /// </summary>
        /// <param name="email"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public bool IsUserExists(string email, string password)
        {
            bool exists = (db.UserJeu.FirstOrDefault(u=>u.Email == email && u.Password == password) != null);
            return exists;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="email"></param>
        /// <param name="password"></param>
        /// <returns>Retourne le nom de l'utilisateur</returns>
        public string Connect(String email, String password)
        {
            string username = string.Empty;
            // USERNAME n'existe pas, on retourne un User à la place ?
            //User user = db.UserJeu.FirstOrDefault(u=>u.);
            return username;
        }

        #endregion

        #region Feed

        /// <summary>
        /// Ajoute un flux rss dans la base de données
        /// </summary>
        /// <param name="title">Titre du flux</param>
        /// <param name="description">Description</param>
        /// <param name="link">Lien</param>
        /// <param name="pubDate">Date de publication du flux</param>
        /// <param name="LastBuildDate">Date de dernière modification</param>
        /// <param name="category">Catégorie</param>
        /// <param name="username">Utilisateur à qui appartient ce flux</param>
        public void AddFeed(string title, string description, string link, string pubDate, string LastBuildDate, string category, string username)
        {
            if (!string.IsNullOrEmpty(title) && !string.IsNullOrEmpty(description) && !string.IsNullOrEmpty(link))
            {
                Feed newFeed = new Feed() { Title = title, Description=description, Link = link, PubDate = pubDate,
                                            LastBuildDate = LastBuildDate, Category = category, Username = username };
                db.AddToFeedJeu(newFeed);
                db.SaveChanges();
            }
        }

        /// <summary>
        /// Retourne tous les feeds de la base de données
        /// </summary>
        /// <returns></returns>
        public List<string> GetAllFeeds()
        {
            List<string> feedsname = new List<string>();
            feedsname = db.FeedJeu.Select(f => f.Title).ToList();
            return feedsname;
        }

        /// <summary>
        /// Retourne tous les feeds d'un utilisateur
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        public List<string> GetAllUserFeed(string email)
        {
            List<string> feedsname = new List<string>();
            feedsname = db.UserFeedJeu.Where(uf => uf.User.Email == email).Select(uf => uf.Feed.Title).ToList();
            return feedsname;
        }


        /// <summary>
        /// Retourne de les feeds de cattégorie "category" de l'utilisateur
        /// </summary>
        /// <param name="email"></param>
        /// <param name="category"></param>
        /// <returns></returns>
        public List<string> GetAllUserFeedByCategory(string email, string category)
        {
            List<string> feedsname = new List<string>();
            feedsname = db.UserFeedJeu.Where(uf => uf.User.Email == email && uf.Feed.Category == category).Select(uf => uf.Feed.Title).ToList();
            return feedsname;
        }

        /// <summary>
        /// Retourne tous les feeds que l'utilisateur à envoyer
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        public List<string> GetUserSenderFeeds(string email)
        {
            List<string> feedsname = new List<string>();
            feedsname = db.DatasExchangedJeu.Where(de=>de.UserSender.Email == email).Select(de => de.Feed.Title).ToList();
            return feedsname;
        }


        /// <summary>
        /// Retourne tous les feeds que l'utilisateur à reçu
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        public List<string> GetUserReceiverFeeds(string email)
        {
            List<string> feedsname = new List<string>();
            feedsname = db.DatasExchangedJeu.Where(de => de.UserReceiver.Email == email).Select(de => de.Feed.Title).ToList();
            return feedsname;
        }

        /// <summary>
        /// Retourne le statut feeds de l'utilisateur à true
        /// </summary>
        /// <param name="fieldTitle"></param>
        /// <param name="email"></param>
        /// <returns></returns>
        public bool IsFeedRead(string fieldTitle, string email)
        {
            // BASE DE DONNEES A REVOIR
            return false;
        }


        /// <summary>
        /// Change l'état du statut feeds de l'utilisateur à true
        /// </summary>
        /// <param name="fieldTitle"></param>
        /// <param name="email"></param>
        /// <returns></returns>
        public bool SetReadFeedState(string fieldTitle, string email)
        {
            // BASE DE DONNEES A REVOIR
            return false;
        }

        #endregion

        #region UserFeed

        /// <summary>
        /// Récupère la liste de tous les feeds de l'utilisateur
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public List<Feed> GetAllUserFeed(User user)
        {
            List<Feed> feeds = new List<Feed>();
            if (user != null)
            {
                foreach (UserFeed userFeed in db.UserFeedJeu.Where(f => f.User.ID == user.ID))
                    feeds.Add(userFeed.Feed);
            }
            return feeds;
        }

        /// <summary>
        /// Partage le flux rss de sender avec addressee
        /// </summary>
        /// <param name="sender">Pas encore utiliser...</param>
        /// <param name="addressee">Destinataire du flux</param>
        /// <param name="feedToShare">Flux à partager</param>
        public void ShareFeed(User sender, User addressee, Feed feedToShare)
        {
            if (db.UserFeedJeu != null && sender != null && addressee != null && feedToShare != null)
            {
                UserFeed userFeed = db.UserFeedJeu.FirstOrDefault(uf=>uf.User.ID == addressee.ID && uf.Feed.ID == feedToShare.ID);
                if (userFeed == null)
                {
                    userFeed = new UserFeed();// { User.ID = addressee.ID, Feed.ID = feedToShare.ID };
                    userFeed.User.ID = addressee.ID;
                    userFeed.Feed.ID = feedToShare.ID;

                    db.AddToUserFeedJeu(userFeed);
                    db.SaveChanges();
                }
            }
        }

        #endregion
    }
}
